// 用户id
let uid = localStorage.getItem("uid");
// console.log("uid"+uid);
// 6-1 购物车列表
function loadCartList() {
  let cartListAPI = "http://jx.xuzhixiang.top/ap/api/cart-list.php";
  axios.get(cartListAPI, { params: { id: uid } }).then((r) => {
    // console.log(r.data);
    let cartArr = r.data.data;
    // console.log(cartArr);
    //   循环遍历
    //   map 对象数组 映射为 字符串数组
    let liArr = cartArr.map(
      (v) => `
        
        <div>
            <div id="jtb" >
                <input type="checkbox" onclick=selectItem() class="check">
            </div>
            <div>
                <img src="${v.pimg}" alt="">
            </div>
            <div id="pname">${v.pname}</div>
            <div id="ipt"><input min="1" onchange="pnumChange(${v.pid},this)" type="number" name="" id=""  value="${v.pnum}"/></div>
            <div id="pprice">￥${v.pprice}</div>
            <div id="btn">
                <button onclick="delBtnClick(${v.pid},this)">删除</button>
                
            </div>
        </div>
        
      `
    );
        // <li>
        //     <h2>${v.pname}</h2>
        //     <p>${v.pprice}</p>
        //     <input min="1" onchange="pnumChange(${v.pid},this)" type="number" name="" id=""  value="${v.pnum}"/>
        //     <button onclick="delBtnClick(${v.pid},this)">删除</button>
        // </li>
    //   let liArr = cartArr.map((v) => {
    //     return `
    //     <li>
    //         <h2>${v.pname}</h2>
    //         <p>${v.pprice}</p>
    //         <input type="number" name="" id=""  value="${v.pnum}"/>
    //         <button>删除</button>
    //     </li>
    //   `;
    //   });
    // console.log(liArr);
    //多个li标签链接成一个字符串 给 ul
    document.querySelector(".clist").innerHTML = liArr.join("");
  });
}

loadCartList();

//   删除
async function delBtnClick(pid, btn) {
  // console.log(pid, btn);

  let url = "http://jx.xuzhixiang.top/ap/api/cart-delete.php";
  // axios.get(url, { params: { uid, pid } }).then((r) => {
  //   console.log(r.data);
  //   //   删除成功
  //   btn.parentNode.remove();
  // });

  await axios.get(url, { params: { uid, pid } });
  (btn.parentNode).parentNode.remove();
  
}

//   修改
function pnumChange(pid, ipt) {
  // console.log(pid, ipt);
  //内容改变事件
  let pnum = ipt.value;
  // console.log("pnum"+pnum);
  let url = "http://jx.xuzhixiang.top/ap/api/cart-update-num.php";
  axios
    .get(url, {
      params: {
        pnum,
        uid,
        pid,
      },
    })
    .then((r) => {
      //修改成功
      // console.log(r.data);
    });
}

    //全选反选
    var jcbAll=document.getElementById('jcbAll');//全选
    // console.log("jcbAll"+jcbAll);
    var jtbs=document.getElementsByClassName('check');//.querySelector("input")//所有的复选框
    // console.log("jtbs"+jtbs);
    jcbAll.onclick=function(){
        //jtbs=document.getElementsByClassName('check');//.querySelector("input")//所有的复选框
        // console.log(111);
        for(var i=0;i<jtbs.length;i++){
            jtbs[i].checked=this.checked;
        }
    }

      function selectItem(){
        for(let i=0;i<jtbs.length;i++){
          // console.log(jtbs[i])
            // jtbs[i].onclick=function(){
            //     console.log(111);
            //     console.log("jtbs[i]"+jtbs[i]);
                var flag=true;
                
                for(var j=0;j<jtbs.length;j++){
                    if(!jtbs[j].checked){
                        flag=false;
                        break;
                    }
                }
                jcbAll.checked=flag;
            }
        }
    
  //商品的总价格

    


